﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;

namespace GUI
{
    public partial class lopadmin : Form
    {
        int i = 0;
        public lopadmin()
        {
            InitializeComponent();
        }

        private void lopadmin_Load(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            cbblop.SelectedIndex = -1;
            lstView.Columns.Add("Mã lớp");
            lstView.Columns.Add("Tên Lớp");
            lstView.Columns.Add("Sỉ số");
            lstView.Columns.Add("Giáo viên chủ nhiệm");
            lstView.Columns.Add("Khối");
            lstView.Columns.Add("Trạng thái");
            DataTable dtGV = GiaoVienBUS.GetTableGV();
            DataTable dtKhoi = hocsinhBUS.danhSachKhoi();
            DataTable dtNH = hocsinhBUS.danhSachNamHoc();
            cbbGiaoVien.DataSource = dtGV;
            cbbGiaoVien.DisplayMember = "TenGV";
            cbbGiaoVien.ValueMember = "MaGV";
            cbbGiaoVien.SelectedIndex = 0;
            cbbKhoi.DataSource = dtKhoi;
            cbbKhoi.DisplayMember = "TenKhoi";
            cbbKhoi.ValueMember = "MaKhoi";
            cbbKhoi.SelectedIndex = 0;
            cbbNamHoc.DataSource = dtNH;
            cbbNamHoc.DisplayMember = "TenNamHoc";
            cbbNamHoc.ValueMember = "MaNH";
            cbbNamHoc.SelectedIndex = 0;
            cbbTrangThai.SelectedIndex = 0;
        }
        private void LamMoi()
        {
            txtMaLop.Text = "";
            txtTenLop.Text = "";
            txtSiSo.Text = "";
            cbbGiaoVien.SelectedIndex = 0;
            cbbKhoi.SelectedIndex = 0;
            cbbNamHoc.SelectedIndex = 0;
            cbbTrangThai.SelectedIndex = 0;
        }
        private void LoadLvw(DataTable dtLp)
        {
            ListViewItem li;
            DataTable dtGV = GiaoVienBUS.GetTableGV();
            DataTable dtKhoi = hocsinhBUS.danhSachKhoi();
            foreach (DataRow r in dtLp.Rows)
            {
                li = new ListViewItem();
                li.Text = r["malop"].ToString();
                li.SubItems.Add(r["tenlop"].ToString());
                li.SubItems.Add(r["siso"].ToString());
                foreach (DataRow r1 in dtGV.Rows)
                {
                    if (r["magv"].ToString() == r1["magv"].ToString())
                    {
                        li.SubItems.Add(r1["TenGV"].ToString());
                    }
                    if (r["magv"].ToString() == r1["magv"].ToString())
                        break;
                }
                foreach (DataRow r2 in dtKhoi.Rows)
                {
                    if (r["MaKhoi"].ToString() == r2["MaKhoi"].ToString())
                        li.SubItems.Add(r2["TenKhoi"].ToString());
                }
                li.SubItems.Add(r["trangthai"].ToString());
                lstView.Items.Add(li);
            }
        }

        private void cbblop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbblop.SelectedIndex != -1)
            {
                lstView.Items.Clear();
                DataTable dtb = CustomBUS.GetLopTheoStatus(cbblop.Text);
                LoadLvw(dtb);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            i = 1;
            btnLuu.Enabled = true;
            btnThem.Enabled = false;
            btnSua.Enabled = true;
            txtMaLop.Enabled = true;
        }
        private void lstView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (i == 2)
            {
                DataTable dtLop = hocsinhBUS.danhsachlop();
                foreach (ListViewItem lis in lstView.SelectedItems)
                {
                    foreach (DataRow r in dtLop.Rows)
                    {
                        if (lis.Text == r["MaLop"].ToString())
                        {
                            txtMaLop.Text = r["MaLop"].ToString();
                            txtTenLop.Text = r["TenLop"].ToString();
                            txtSiSo.Text = r["SiSo"].ToString();
                            cbbGiaoVien.SelectedValue = r["MaGV"].ToString();
                            cbbKhoi.SelectedValue = r["MaKhoi"].ToString();
                            cbbNamHoc.SelectedValue = r["MaNH"].ToString();
                            cbbTrangThai.Text = r["TrangThai"].ToString();
                        }
                    }
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lstView.SelectedItems.Count == 0)
                MessageBox.Show("Chọn một lớp trong danh sách để xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                DataTable dtLop = hocsinhBUS.danhsachlop();
                foreach (ListViewItem li in lstView.SelectedItems)
                    foreach (DataRow r in dtLop.Rows)
                    {
                        if (r["MaLop"].ToString() == li.Text)
                        {
                            string ten = r["TenLop"].ToString();
                            DialogResult d = MessageBox.Show("Xóa " + r["TenLop"].ToString(), "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (d == DialogResult.Yes)
                            {
                                r.Delete();
                                if (CustomBUS.FTableDirect(dtLop, "LOP"))
                                {
                                    MessageBox.Show("Đã xóa " + ten, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    cbblop_SelectedIndexChanged(sender, e);
                                    break;
                                }
                            }

                        }
                    }

            }
        }

        private void btnSua_Click_1(object sender, EventArgs e)
        {
            i = 2;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            btnThem.Enabled = true;
            txtMaLop.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (i == 1)
            {
                if (txtMaLop.Text.Length == 0 || txtTenLop.Text.Length == 0)
                {
                    MessageBox.Show("Chưa nhập đủ mã lớp và tên lớp!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    DataTable dtLop = hocsinhBUS.danhsachlop();
                    DataRow dr = dtLop.NewRow();
                    dr["MaLop"] = txtMaLop.Text;
                    dr["TenLop"] = txtTenLop.Text;
                    dr["SiSo"] = Convert.ToInt32(txtSiSo.Text);
                    dr["MaGV"] = cbbGiaoVien.SelectedValue;
                    dr["MaKhoi"] = cbbKhoi.SelectedValue;
                    dr["TrangThai"] = cbbTrangThai.Text;
                    dr["MaNH"] = cbbNamHoc.SelectedValue;
                    dtLop.Rows.Add(dr);
                    if (CustomBUS.FTableDirect(dtLop, "LOP"))
                    {
                        MessageBox.Show("Thêm thành công " + txtTenLop.Text);
                        cbblop_SelectedIndexChanged(sender, e);
                        LamMoi();
                    }
                }
            }
            if (i == 2)
            {
                if (txtMaLop.Text.Length == 0)
                    MessageBox.Show("Chọn một lớp trong danh sách để sửa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                {
                    DataTable dtLop = hocsinhBUS.danhsachlop();
                    foreach (DataRow r in dtLop.Rows)
                    {
                        if (r["MaLop"].ToString() == txtMaLop.Text)
                        {
                            r.BeginEdit();
                            r["TenLop"] = txtTenLop.Text;
                            r["TenLop"] = txtTenLop.Text;
                            r["SiSo"] = Convert.ToInt32(txtSiSo.Text);
                            r["MaGV"] = cbbGiaoVien.SelectedValue;
                            r["MaKhoi"] = cbbKhoi.SelectedValue;
                            r["TrangThai"] = cbbTrangThai.Text;
                            r["MaNH"] = cbbNamHoc.SelectedValue;
                            r.EndEdit();
                            if (CustomBUS.FTableDirect(dtLop, "LOP"))
                            {
                                MessageBox.Show("Sửa thành công " + txtTenLop.Text, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                cbblop_SelectedIndexChanged(sender, e);
                                LamMoi();
                            }
                        }
                    }
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Thoát ?", "Xác nhận thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
                this.Close();
        }
    }
}